#include "STC8G_H_Delay.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_Switch.h"
#include "STC8G_H_ADC.h"

#include "SHTC3.h"
#include "ssd1312.h"
#include "key.h"
#include "adc.h"

#include "config.h"
#include <stdio.h>
#include <string.h>
#include <intrins.h>
extern u8 TEMP_DATA_Cont;
extern u8 TEMP_DATA[10];
extern u8 HUMI_DATA[10];

extern u8 adc_cont;
extern u16 ADC_DATA[10];
u8 adc_hcont = 0;
u8 adc_lcont = 0;

u8 PicChange = 1;
u8 BackLight = 1;
u8 KeyCount;
extern code u8 heart[128];
extern code u8 mi[128];
extern code u8 star[128];
 

void show_tem(void)
{
	OLED_ShowChar(4,0,':',32); //第十位
	OLED_ShowChar(4,4,';',32); //第11位
	show32_2point(24,0);
	show32_2point(24,4);
	show32_icon(94,4,star);

	if (TEMP_DATA_Cont == 0)
		show32_icon(94,0,mi);
 	else 
	 	show32_icon(94,0,heart);
	OLED_ShowString(36,0,TEMP_DATA,32);
	OLED_ShowString(36,4,HUMI_DATA,32);
	OLED_ShowChar(68+4,4,'<',32); //第是12位

}
void show_adc(void)
{
 	if (adc_cont == 1)  
	{
		adc_lcont = 1;
		OLED_ShowString(8,0,ADC_DATA,64);
	}
	
	if(adc_cont == 0)
	{
		adc_hcont = 1;
		if(adc_lcont == 1)		//需要刷新，三字符  9.1
		{
			OLED_Clear();
			adc_lcont = 0;
		}
		OLED_ShowString(24,0,ADC_DATA,64);
		
	}
	

}

void UART_config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	COMx_InitDefine COMx_InitStructure; // 结构定义

	GPIO_InitStructure.Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitStructure.Mode = GPIO_PullUp;
	GPIO_Inilize(GPIO_P3, &GPIO_InitStructure);

	COMx_InitStructure.UART_Mode = UART_8bit_BRTx;	// 模式, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
	COMx_InitStructure.UART_BRT_Use = BRT_Timer1;	// 选择波特率发生器, BRT_Timer1, BRT_Timer2 (注意: 串口2固定使用BRT_Timer2)
	COMx_InitStructure.UART_BaudRate = 115200ul;	// 波特率, 一般 110 ~ 115200
	COMx_InitStructure.UART_RxEnable = ENABLE;		// 接收允许,   ENABLE或DISABLE
	COMx_InitStructure.BaudRateDouble = DISABLE;	// 波特率加倍, ENABLE或DISABLE
	UART_Configuration(UART1, &COMx_InitStructure); // 初始化串口1 UART1,UART2,UART3,UART4
	NVIC_UART1_Init(ENABLE, Priority_1);			// 中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3

	UART1_SW(UART1_SW_P30_P31); // UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44
}

void main(void)
{
	char aa[3] = 0;
	u8 tim, i = 0;
 
	EAXSFR(); /* 扩展寄存器访问使能 */
	UART_config();
	// P10 = 1;   //adc
	EA = 1;
	OLED_Init();
	SHTC3_Init();
	ADC_Init();
	key_init();
	delay_ms(10);
	while (1)
	{
		if (tim > 10)
		{
			SHTC3_ReadTemp();
			ADC_GET();
			tim = 0;
 			// printf("aa\r\n");	
		}
		KeyCount = KEY_Scan();
        if (KeyCount)
        {

            if (KeyCount == KEY1_PRES)
            {
                BackLight = !BackLight;
                if (BackLight == 0)
					OLED_SW = 0;
                else
					OLED_SW = 1;
				//printf("KEY0_PRES = %bd\r\n",KeyCount);
            }
            if (KeyCount == KEY0_PRES)
            {
                PicChange = !PicChange;
				OLED_Clear();
				// printf("KEY1_PRES = %bd\r\n",KeyCount);
            }
        }
		if (PicChange == 0)
			show_tem();
		else 	
			show_adc();
		tim++;
		delay_ms(10);
 

	}
}